package com.auxgroup.hr.front.site.co.process;

import com.auxgroup.hr.front.commons.BaseRetCode;
import com.auxgroup.hr.front.commons.vo.BaseResponseVO;
import com.auxgroup.hr.front.commons.vo.UserDTO;
import com.auxgroup.hr.front.dao.domain.PsCWfCerHdr;
import com.auxgroup.hr.front.dozer.IObjectMapper;
import com.auxgroup.hr.front.service.process.PsCAppStepPmiService;
import com.auxgroup.hr.front.service.process.WfCerHdrService;
import com.auxgroup.hr.front.site.co.BaseWfCO;
import com.auxgroup.hr.front.site.inteceptor.SessionGain;
import com.auxgroup.hr.front.site.vo.process.WfCerHdrReq;
import com.auxgroup.hr.front.site.vo.process.WfCerHdrRespVO;
import com.auxgroup.hr.front.ws.ProcessApplyQueDTO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Set;

/**
 * 证明开具流程
 * 
 * @author wangchangyun
 * @version 创建时间：180601
 */
@RestController
@RequestMapping("${auxgroup.hr.contextPath}/wfCerHdr")
public class WfCerHdrCO extends BaseWfCO {

	private static final Logger logger = LoggerFactory.getLogger(WfCerHdrCO.class);

	@Autowired
	public WfCerHdrService wfCerHdrService;

	@Resource(name = "dozerMapper")
	protected IObjectMapper objectMapper;

	@Autowired
	public PsCAppStepPmiService psCAppStepPmiService;
	

	/**
	 * 查询证明开具流程
	 * 
	 * @param hireId
	 * @return
	 */
	@GetMapping(value = "/getOne")
	public BaseResponseVO getOne(String cTransactionNbr,@SessionGain("user") UserDTO user) {
		logger.info("getWfCerHdr param= {}", cTransactionNbr);
		PsCWfCerHdr wfCerHdr = wfCerHdrService.getWfCerHdr(cTransactionNbr);
		if (wfCerHdr == null) {
			return buildRespone(BaseRetCode.CODE_ERROR_PROCESS_NOT_EXIST);
		}
		WfCerHdrRespVO respVo = objectMapper.map(wfCerHdr, WfCerHdrRespVO.class);
		fillBaseWfInfo(user, wfCerHdr, respVo);
		//证明开具流程的字段在审批时全部是只读，不需要权限控制
		return buildRespone(respVo);
	}

	/**
	 * 开具证明信息录入
	 * 
	 * @param hireId
	 * @return
	 */
	@PostMapping(value = "/save")
	public BaseResponseVO save(@SessionGain("user") UserDTO userInfo,WfCerHdrReq cerHdrReq) {
		logger.info("saveWfCerHdr param= {}", cerHdrReq);
		if(StringUtils.isBlank(cerHdrReq.getcTransactionNbr())){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL.getRetCode(), "流程编号不能为空");
		}
		PsCWfCerHdr wfCerHdr = objectMapper.map(cerHdrReq, PsCWfCerHdr.class);
		wfCerHdr = wfCerHdrService.save(wfCerHdr,userInfo);
		if (wfCerHdr == null) {
			return buildRespone(BaseRetCode.CODE_ERROR_PROCESS_APRROVE_FAIL);
		}
		//调用发起流程接口
		ProcessApplyQueDTO requestDTO = new ProcessApplyQueDTO();
		requestDTO.setC_WF_TYPE(wfCerHdr.getcWfType());
		requestDTO.setC_TRANSACTION_NBR(wfCerHdr.getcTransactionNbr());
		requestDTO.setC_ACTION_TYPE("Submit");
		requestDTO.setC_USERID(userInfo.getOprId());
		requestDTO.setC_COMMENTS(cerHdrReq.getComments());
		Map<String, String> resp = processService.apply(requestDTO);
		if (resp == null) {
			return buildRespone(BaseRetCode.CODE_ERROR_PROCESS_APRROVE_FAIL);
		}
		return buildRespone(resp.get("C_MSG_CODE"), resp.get("C_MSG_TEXT"));
	}

	@Override
	protected Set<String> getAuthFileds() {
		return null;
	}
}
